
$(function () {
    // 初始化数据
    initData();
    // 动态调整列宽
    $("table").colResizable();
    // 按照条件筛选事件
    $(".btn-primary").click(function () {
        selectByConditions();
    });
});

function initData() {
    // 时间筛选狂
    $('#datetimepicker1, #datetimepicker2').datetimepicker({
        // minView: "month", //选择日期后，不会再跳转去选择时分秒
        language:  'zh-CN',
        format: 'yyyy-mm-dd hh:ii:ss',
        todayBtn:  1,
        autoclose: 1
    });
    // 事件owner 筛选框
    $("#eventOwner").html("");
    var ownerDiv = '<option value="">请选择...</option>';
    $.each(event_owner, function (owner, mail) {
        ownerDiv += '<option value="' + owner + '">' + owner + '</option>';
    });
    $("#eventOwner").append(ownerDiv);
    // 事件type 筛选框
    $("#eventType").html("");
    var typeDiv = '<option value="">请选择...</option>';
    $.each(all_event, function (eventSort, eventType) {
        // $.each(eventType, function (owner, mail) {
        $.each(eventType, function (i, type) {
            typeDiv += '<option value="' + type+'">'+type+'</option>';
        });
    });
    $("#eventType").append(typeDiv);
    selectByConditions();

}

function selectByConditions() {
    var eventSort = $("#eventSort").val();
    var eventOwner = $("#eventOwner").val();
    var eventState = $("#eventState").val();
    var eventType = $("#eventType").val();
    var threatLevel = $("#threatLevel").val();
    var earliest = $("#earliest").val();
    var latest = $("#latest").val();
    pageEvents(1, 15, eventSort, eventOwner, eventState,  eventType, threatLevel, earliest, latest);
}

function pageEvents(pageNo, pageSize, eventSort, eventOwner, eventState, eventType, threatLevel, earliest, latest) {

    $("#eventTable").empty();
    clearAll();
    var columns = [{code:"eventId", name:"checkBox", show:false},
        {code:"eventId", name:"事件编号/ID"},
        {code:"eventSort", name:"事件分类"},
        {code:"eventType", name:"事件分级"},
        {code:"createTimeStr", name:"创建时间"},
        {code:"eventOwner", name:"owner"},
        {code:"threatLevelStr", name:"威胁级别"},
        {code:"eventDeptName", name:"处理部门"},
        {code:"eventStateStr", name:"事件状态"},
        {code:"alertCount", name:"告警数量"},
        {code:"isSendEmailStr", name:"邮件是否发送"},
        // {code:{S:"查看告警列表",E:"编辑事件",C:"关闭事件", D:"删除事件"}, name:"操作"}];
        {code:{S:"查看告警列表",E:"编辑事件",C:"关闭事件"}, name:"操作"}];
    var params = {url:basePath + "/splunk/event/getEventByConditions.do", divID:"eventTable", afterQuery:"adjustTable", message:"暂无事件，请先创建事件或者更改搜索条件"};
    var data = {pageNo:pageNo, pageSize:pageSize, eventSort:eventSort, eventOwner:eventOwner, eventState:eventState, eventType:eventType, threatLevel:threatLevel, earliest:earliest, latest:latest};
    easyQuery(columns, params, data);
}

// 调整table页
function adjustTable() {

    // 如果事件为关闭状态，操作一列只显示删除按钮
    $("#eventTable tr").each(function (i, n) {
        if (i != 0) {
            var eventStateStr = $(this).find("td:eq(8)").attr("title");
            if (eventStateStr == "已关闭") {
                $(this).find("td:eq(11)").find("span[title='编辑事件']").remove();
                $(this).find("td:eq(11)").find("span[title='关闭事件']").remove();
            }
            $(this).find("td[class!='operation']").click(function () {
                // 先清除所有的选中状态
                clearAll();
                var dom = $(this).parents("tr").find("td[class='checkNot color']").find("input");
                if ($(dom).is(":checked")) {
                    $(dom).prop("checked", false);
                }else{
                    $(dom).prop("checked", "checked");
                }
                manucheck(dom);
                showAlerts(dom);
            });
            $.each($(this).find("td[class!='operation']"), function () {
                $(this).addClass("color");
            })

        }

    });

}

// 查看告警数据
function see_eventTable(btn) {
    var eventId = $(btn).parents("tr").attr("nodeid");
    layer.open({
        type: 2,
        area: ['800px', '480px'],
        maxmin: true,
        title:'告警列表信息',
        offset:"20px",
        shadeClose: true,
        content: basePath + "/splunk/event/initEventAlerts.do?eventId=" + eventId
    });
}

// 编辑事件
function edit_eventTable(btn) {
    var eventId = $(btn).parents("tr").attr("nodeid");
    layer.open({
        type: 2,
        area: ['800px', '480px'],
        maxmin: true,
        title:'告警列表信息',
        offset:"20px",
        shadeClose: true,
        content: basePath + "/splunk/event/initEditEvent.do?eventId=" + eventId
    });
}

// 删除事件
function del_eventTable(btn) {
    var eventId = $(btn).parents("tr").attr("nodeid");
    layer.confirm("确定删除吗？", {btn: ['是', '否']},
        function () {
            $.ajax({
                url: basePath + "/splunk/event/deleteEvent.do",
                type: "post",
                dataType: "json",
                data: {
                    eventId: eventId
                },
                success: function (json) {
                    var code = json.code;
                    if (code == '200') {
                        layer.msg("已删除", {time: 1500});
                        initData();
                    } else if (code == '500') {
                        layer.msg("删除失败", {time: 1500});
                    }
                }
            })
        }
    );
}

// 关闭事件
function close_eventTable(btn){
    var eventId = $(btn).parents("tr").attr("nodeid");
    layer.open({
        type: 2,
        area: ['800px', '480px'],
        maxmin: true,
        title:'关闭事件',
        offset:"20px",
        shadeClose: true,
        content: basePath + "/splunk/event/initCloseEvent.do?eventId=" + eventId
    });
}


// table页 第一列单选框触发事件
function manucheck(input) {
    if ($(input).is(":checked")){
        $(input).parents("tr").addClass("timeoutTr");
    }else {
        $(input).parents("tr").removeClass("timeoutTr");
    }
}

function clearAll() {
    clearAllTable();

    // 清除所有的选中状态
    $("#eventTable tr").each(function (i, n) {
        if (i != 0) {
           $(this).find("td[class='checkNot']").find("input").prop("checked",false);
           $(this).removeClass("timeoutTr");
        }

    });
}

function showAlerts(input) {
    var eventId = $(input).val();
    var columns = [{code:"alertSid", name:"checkBox", show:false},
        {code:"tempAlertName", name:"告警名称"},
        {code:"triggerTimeStr", name:"告警触发时间"},
        {code:"alertType", name:"告警类型"},
        {code:"alertOwner", name:"告警所属者"},
        {code:"alertLevelInfo", name:"告警级别"},
        {code:"alertStateInfo", name:"告警状态"},
        // {code:"alertCount", name:"告警数量"},
        {code:"eventSorts", name:"关联事件名称"},
        {code:{S:"查看日志",I:"查看更多告警", EV:"查看绑定事件", SE:"搜索"}, name:"操作"}];
    var param = {url:basePath + "/splunk/event/getAlertsByEventId.do",divID:"alertTable",afterQuery:"adjustAlertTable", message:"暂时没有告警信息"};
    var data = {"pageNo":1, "pageSize":15, eventId:eventId};
    easyQuery(columns, param, data);
}
